package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.AddAdvertisementRequest;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.StatusRequest;
import com.sc.nft.admin.request.UpdateAdvertisementRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dto.CreateSelfAmmunitionPageDTO;
import com.sc.nft.dto.EditSelfAmmunitionPageDTO;
import com.sc.nft.entity.AdvertisementInfo;
import com.sc.nft.entity.SelfAmmunition;
import com.sc.nft.entity.vo.AdminAdvertisementInfoVO;
import com.sc.nft.entity.vo.SelfAmmunitionDetailVO;
import com.sc.nft.service.AdvertisementInfoService;
import com.sc.nft.service.SelfAmmunitionService;
import com.sc.nft.service.UserCityServerCentreService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 菜单表服务控制器
 *
 * @author rdg
 * @since 2022-06-05
 * @description
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/advertisement")
@Api(tags="banner管理")
public class AdvertisementInfoController {

    private final AdvertisementInfoService advertisementInfoService;
    private final SelfAmmunitionService selfAmmunitionService;


    @PostMapping(value="/listAdvertisement", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取banner列表",notes="获取banner列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminAdvertisementInfoVO> listAdvertisement(@Valid @RequestBody PageRequest pageRequest){
        Page<AdvertisementInfo> page = advertisementInfoService.listAdvertisement(pageRequest.getPageNo(),pageRequest.getPageSize());
        IPage<AdminAdvertisementInfoVO> convert = page.convert(advertisementInfo -> {
            AdminAdvertisementInfoVO adminAdvertisementInfoVO = new AdminAdvertisementInfoVO();
            BeanUtils.copyProperties(advertisementInfo, adminAdvertisementInfoVO);
            adminAdvertisementInfoVO.setRank(advertisementInfo.getSortRule());
            return adminAdvertisementInfoVO;
        });
        return PageResult.success(convert);
    }

    @PostMapping(value="/addAdvertisement", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增banner",notes="新增banner", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result addAdvertisement(@Valid @RequestBody AddAdvertisementRequest addAdvertisementRequest){
        AdvertisementInfo advertisementInfo=new AdvertisementInfo();
        BeanUtils.copyProperties(addAdvertisementRequest,advertisementInfo);
        advertisementInfo.setSortRule(addAdvertisementRequest.getRank());
        return Result.success(advertisementInfo.insert());
    }

    @PostMapping(value="/updateAdvertisement", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改banner",notes="修改banner", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result updateAdvertisement(@Valid @RequestBody UpdateAdvertisementRequest updateNoticeInfoRequest){
        AdvertisementInfo advertisementInfo=new AdvertisementInfo();
        BeanUtils.copyProperties(updateNoticeInfoRequest, advertisementInfo);
        advertisementInfo.setSortRule(updateNoticeInfoRequest.getRank());
        return Result.success(advertisementInfo.updateById());
    }

    @PostMapping(value = "/deleteAdvertisement", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除banner", notes = "删除banner", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result deleteAdvertisement(@Valid @RequestBody IdRequest idRequest) {
        new AdvertisementInfo().deleteById(idRequest.getId());
        return Result.success();
    }

    @PostMapping(value = "/selfAmmunitionAdd", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增自弹", notes = "新增自弹", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result selfAmmunitionAdd(@Valid @RequestBody CreateSelfAmmunitionPageDTO request) {
        selfAmmunitionService.selfAmmunitionAdd(request);
        return Result.success();
    }

    @PostMapping(value = "/selfAmmunitionUpdate", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改自弹", notes = "修改自弹", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result selfAmmunitionUpdate(@Valid @RequestBody EditSelfAmmunitionPageDTO request) {
        selfAmmunitionService.selfAmmunitionUpdate(request);
        return Result.success();
    }

    @PostMapping(value = "/selfAmmunitionPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自弹列表", notes = "自弹列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult selfAmmunitionPage(@Valid @RequestBody PageRequest request) {
        return PageResult.success(selfAmmunitionService.selfAmmunitionPage(request.toMp()));
    }

    @PostMapping(value = "/selfAmmunitionDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自弹详情", notes = "自弹详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<SelfAmmunitionDetailVO> selfAmmunitionDetail(@Valid @RequestBody IdRequest request) {
        return Result.success(selfAmmunitionService.selfAmmunitionDetail(request.getId()));
    }

    @PostMapping(value = "/selfAmmunitionDelete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自弹删除", notes = "自弹删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<SelfAmmunitionDetailVO> selfAmmunitionDelete(@Valid @RequestBody IdRequest request) {
        selfAmmunitionService.selfAmmunitionDelete(request.getId());
        return Result.success();
    }

    @PostMapping(value = "/selfAmmunitionIsListing", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自弹上下架", notes = "自弹上下架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<SelfAmmunitionDetailVO> selfAmmunitionIsListing(@Valid @RequestBody StatusRequest request) {
        selfAmmunitionService.selfAmmunitionIsListing(request.getId(),request.getStatus());
        return Result.success();
    }



}
